package com.jwthhealth_pub.old.acupressure.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.acupressure.module.AcupressureJlpicModule;
import com.jwthhealth_pub.old.acupressure.view.adapter.MeridianAdapter;
import com.jwthhealth_pub.old.api.ApiHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/12.
 */
public class MeridianActivity extends Activity {
    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private List<AcupressureJlpicModule.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meridian);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        initTop();

        Call<AcupressureJlpicModule> acupressureJlpicModuleCall = ApiHelper.acupressureList();
        acupressureJlpicModuleCall.enqueue(new Callback<AcupressureJlpicModule>() {

            @Override
            public void onResponse(Call<AcupressureJlpicModule> call, Response<AcupressureJlpicModule> response) {
                AcupressureJlpicModule body = response.body();
                if (body.getCode().equals("0")) {
                    data = body.getData();
                    handler.sendEmptyMessage(0);
                } else {
                    Log.d("MeridianActivity", "fail");
                }
            }

            @Override
            public void onFailure(Call<AcupressureJlpicModule> call, Throwable t) {
                Log.d("MeridianActivity", "error");
            }
        });

    }

    private void initData() {
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new MeridianAdapter(this, data));
    }

    private void initTop() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressbar.setVisibility(View.GONE);
            if (msg.what == 0) {
                initData();
            }
        }
    };
}
